<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;

use think\db\Connection;
use think\db\Query;
use think\Db;
class Wxinpay
{
   public function __construct()
   {

   }
   /*统一下单口*/
   public function order($array)
   {
      $key='9b2a0c0513e9b219177457c7ad136797';
      $url="";
      $rand=rand(100,10000);
      $appid='wxb31beb33ba213db6';
      $mch_id=1501380151;
      $total_fee=$array['actual_money'];
      $out_trade_no=$array['order_no'].$rand;
      $nonce_str=md5($out_trade_no);
      $body="加油";
      $spbill_create_ip=$this->get_client_ip();;
      $notify_url=$url;
      $trade_type='MWEB';
      $scene_info='{"h5_info":{"type":"Wap","wap_url":"hyxt.w209.madgg.com","wap_name":"收费系统"}}';//场景信息 必要参数
      $signA ="appid=$appid&attach=$out_trade_no&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
       $strSignTmp = $signA."&key=$key";
       $sign = strtoupper(MD5($strSignTmp));
       $post_data = "<xml>
                   <appid>$appid</appid>
                   <mch_id>$mch_id</mch_id>
                   <body>$body</body>
                   <out_trade_no>$out_trade_no</out_trade_no>
                   <total_fee>$total_fee</total_fee>
                   <spbill_create_ip>$spbill_create_ip</spbill_create_ip>
                   <notify_url>$notify_url</notify_url>
                   <trade_type>$trade_type</trade_type>
                   <scene_info>$scene_info</scene_info>
                   <attach>$out_trade_no</attach>
                   <nonce_str>$nonce_str</nonce_str>
                   <sign>$sign</sign>
               </xml>";//拼接成XML 格式
       $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
       $dataxml = $this->postXmlCurl($post_data,$url);
       $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML 转换成数组
       var_dump($objectxml);
   }
    public function postXmlCurl($xml,$url,$second = 30){
        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //运行curl
        $data = curl_exec($ch);
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        }else{
            $error = curl_errno($ch);
            curl_close($ch);
            echo "curl出错，错误码:$error"."<br>";
        }
    }
    /*获取IP*/
    public function get_client_ip() {
        static $realip;
        if (isset($_SERVER)) {
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }
        return $realip;
    }
}
